package com.huawei.hms.framework.network.restclient.hwhttp;

import android.text.TextUtils;
import com.huawei.hms.framework.common.IoUtils;
import com.huawei.hms.framework.common.Logger;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;

public class ResponseBody implements Closeable {
    private Charset charSet;
    private long contentLength;
    private String contentType;
    private InputStream inputStream;
    private Reader reader;

    public static final class Builder {
        private Charset charSet;
        private long contentLength;
        private String contentType;
        private InputStream inputStream;

        public Builder contentType(String str) {
            this.contentType = str;
            return this;
        }

        public Builder contentLength(long j) {
            this.contentLength = j;
            return this;
        }

        public Builder inputStream(InputStream inputStream) {
            if (inputStream == null) {
                throw new NullPointerException("inputStream not null in ResponseBody");
            }
            this.inputStream = inputStream;
            return this;
        }

        public Builder charSet(String str) {
            Throwable e;
            if (!TextUtils.isEmpty(str)) {
                try {
                    this.charSet = Charset.forName(str);
                } catch (IllegalCharsetNameException e2) {
                    e = e2;
                    Logger.w("ResponseBody", "charSet error", e);
                    return this;
                } catch (UnsupportedCharsetException e3) {
                    e = e3;
                    Logger.w("ResponseBody", "charSet error", e);
                    return this;
                }
            }
            return this;
        }

        public Builder charSet(Charset charset) {
            this.charSet = charset;
            return this;
        }

        public ResponseBody build() {
            return new ResponseBody();
        }
    }

    private ResponseBody(Builder builder) {
        this.contentType = builder.contentType;
        this.contentLength = builder.contentLength;
        this.inputStream = builder.inputStream;
        this.charSet = builder.charSet;
    }

    public void close() throws IOException {
        if (this.inputStream != null) {
            IoUtils.closeSecure(this.inputStream);
        }
        if (this.reader != null) {
            IoUtils.closeSecure(this.reader);
        }
    }

    public String getContentType() {
        return this.contentType;
    }

    public long getContentLength() {
        return this.contentLength;
    }

    public final InputStream getInputStream() {
        return this.inputStream;
    }

    public final byte[] bytes() throws IOException {
        try {
            if (this.contentLength > 2147483647L) {
                throw new IOException("Cannot buffer entire body for content length: " + this.contentLength);
            }
            byte[] bArr;
            if (this.inputStream == null) {
                bArr = new byte[0];
            } else {
                bArr = IoUtils.toByteArray(this.inputStream);
                if (this.contentLength == -1 || this.contentLength == ((long) bArr.length)) {
                    close();
                } else {
                    throw new IOException("Content-Length (" + this.contentLength + ") and stream length (" + bArr.length + ") disagree");
                }
            }
            return bArr;
        } finally {
            close();
        }
    }

    public final Reader charStream() {
        if (this.inputStream == null) {
            return null;
        }
        if (this.reader == null) {
            this.reader = new InputStreamReader(this.inputStream, this.charSet == null ? Charset.forName("UTF-8") : this.charSet);
        }
        return this.reader;
    }
}
